/**
 * Created by panjun7@yunzongnet.com on 2016/10/25.
 */
SuitShopsController = function () {
    this.initPage = function() {
        var paramObj = CommonUtil.takeOutParamObject();

        var serverUrl = App.serverUrl +　"/merchantApp/activity/getActivityShops.do";
        var self = this;
        var sucFunc = function (result) {
            var cityList = [];
            if (String(result.success) == 'true') {
                var resultObject = result.resultObject;
                if(resultObject){
                    self.assembleShopList(paramObj,resultObject);
                    for(var key in resultObject){
                        var cityInfo = {"cityName":key,"shopList":[]};
                        for(var i=0;i<resultObject[key].length;i++){
                            var shopInfo = resultObject[key][i];
                            cityInfo.shopList.push(shopInfo);
                        }
                        cityList.push(cityInfo);
                    }
                    CommonUtil.render($("#suitShopsPage .body"), "suitShopsTpl",{"cityList":cityList,"suitShopsSelect":paramObj.suitShopsSelect});
                }

            } else {
                DialogUtil.dialogWarn(result.msg);
            }
        };

        var failFunc = function () {
            log4JS.info("getStoreList failed. http status not 200");
            DialogUtil.dialogWarn();
        };
        CommonUtil.sendJsonpRequest({"url":serverUrl,"success":sucFunc,"error":failFunc,"params":{"activityMarketType":paramObj.activityType}});
        
    };

    /**
    @param selectedData 已经选择的门店数据
    @param allData 所有的门店数据
    */
    this.assembleShopList = function(selectedShopInfo,allData){
        if(allData){
            // 组装数据
            for(var i in allData){
                for(var j = 0;j<allData[i].length;j++){
                    var shopInfo = allData[i][j];
                    var branchShopName = shopInfo.branchShopName?"（"+shopInfo.branchShopName+"）":"";
                    shopInfo.shopName = shopInfo.mainShopName+branchShopName;
                    
                    if(!selectedShopInfo){
                        continue;
                    }

                    //给门店打checked的标
                    if(selectedShopInfo.suitShopsSelect == 1){
                        shopInfo.checked = true;
                    }else if(selectedShopInfo.suitShops){
                        var suitShopIds = selectedShopInfo.suitShops.split(",");
                        for(var k = 0;k<suitShopIds.length;k++){
                            if(shopInfo.shopId == suitShopIds[k]){
                                shopInfo.checked = true;
                                break;
                            }
                        }
                    }                  
                }
            }
        }
    };

    this.getSelectedShopInfo = function(){
        var suitShopIds = [];
        $(".store-list .zhk-list-wx.checked").each(function(i,n){
            suitShopIds.push($(n).attr("data-shopId"));
        });

        var suitShops = suitShopIds.join(",");

        var suitShopsSelect = 2;
        if($("#suitShopsPage .check-all").hasClass("checked")){
            suitShopsSelect = 1;
        }

        return {'suitShopsSelect':suitShopsSelect,'suitShops':suitShops};
    }
};